﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;
using DeSleeper.Library.Properties;

namespace DeSleeper.Library
{
    public class SelfFlatteningAggregateException : AggregateException
    {
        // Methods
        public SelfFlatteningAggregateException() {}
        public SelfFlatteningAggregateException(IEnumerable<Exception> innerExceptions) : base(innerExceptions) {}
        public SelfFlatteningAggregateException(params Exception[] innerExceptions) : base(innerExceptions) {}

        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
        protected SelfFlatteningAggregateException(SerializationInfo info, StreamingContext context) : base(info, context) {}
        public SelfFlatteningAggregateException(string message, IEnumerable<Exception> innerExceptions) : base(message, innerExceptions) {}
        public SelfFlatteningAggregateException(string message, Exception inner) : base(message, inner) {}
        public SelfFlatteningAggregateException(string message, params Exception[] innerExceptions) : base(message, innerExceptions) { }

        public override string Message
        {
            get
            {
                var builder = new StringBuilder(base.Message);
                for (int i = 0; i < InnerExceptions.Count; i++)
                {
                    builder.AppendFormat(CultureInfo.InvariantCulture,
                                         Resources.AggregateExceptionMessageFormat,
                                         Environment.NewLine, InnerExceptions[i].Message);
                }
                return builder.ToString();
            }
        }
    }
}
